template <typename T>
class UnionFindSet
{
public:
    UnionFindSet()
    {
    }

    template <typename TContainer>
    UnionFindSet(const TContainer &container)
    {
        auto begin = container.begin();
        auto end = container.end();
        while (begin != end)
        {
            m[*begin] = *begin;
            ++begin;
            ++_groupCount;
        }
    }

    void insert(const T &value)
    {
        if (!m.count(value))
        {
            m[value] = value;
            ++_groupCount;
        }
    }

    bool exist(const T &value)
    {
        return m.count(value);
    }

    T find(const T &value)
    {
        if (m[value] == value)
        {
            return value;
        }
        return m[value] = find(m[value]);
    }

    void join(const T &value1, const T &value2)
    {
        if (find(value1) != find(value2))
        {
            m[find(value1)] = find(value2);
            --_groupCount;
        }
    }

    int groupCount()
    {
        return _groupCount;
    }

private:
    std::unordered_map<T, T> m;
    int _groupCount = 0;
};

class Solution
{
public:
    vector<bool> friendRequests(int n, vector<vector<int>> &restrictions, vector<vector<int>> &requests)
    {
        UnionFindSet<int> friendGroups;
        for (int i = 0; i < n; ++i)
        {
            friendGroups.insert(i);
        }

        vector<bool> result;
        for (auto &request : requests)
        {
            bool valid = true;
            int u = request[0];
            int v = request[1];

            if (friendGroups.find(u) == friendGroups.find(v))
            {
                result.push_back(true);
                continue;
            }

            for (auto &restriction : restrictions)
            {
                int group0 = friendGroups.find(restriction[0]);
                int group1 = friendGroups.find(restriction[1]);
                if (friendGroups.find(u) == group0 && friendGroups.find(v) == group1)
                {
                    valid = false;
                    break;
                }
                if (friendGroups.find(u) == group1 && friendGroups.find(v) == group0)
                {
                    valid = false;
                    break;
                }
            }

            if (valid)
            {
                friendGroups.join(u, v);
            }
            result.push_back(valid);
        }
        return result;
    }
};